@echo off
REM ########################################################################################
REM #
REM #	NAME:  lp_hobbit_drive_report.bat
REM #	AUTHOR: Joe Birney - FJB047
REM #	GROUP:  METS
REM #	DATE CREATED: 04/06/2010
REM #
REM # 	PROGRAM PURPOSE:	
REM #   This script is to be used on a windows server to report the size of the L/P drives
REM # 
REM # 	OPTIONS
REM #   Site can use either Gb values for the thresholds or % values. Default is %
REM # 
REM #	ACKNOWLEDGEMENTS: 
REM #
REM #	REVISIONS:
REM #	REV		DATE		INITIALS	DESCRIPTION
REM #	1.0		04/06/2010	Jb			First version
REM #	1.1		06/06/2010	Jb			Removed the need to have mapped L/P drives as using UNC path, moved logic to VBS scripts, removed dependency on DFK
REM #	1.2		07/14/2010	Jb			Tweaked to provide more deailts on when it had errors
REM #	1.3		07/26/2010	Jb			Fixed type in var names used to display free size when USE_GB_VALUES was set to YES
REM #	1.4		08/25/2010	Jb			Tweaked the display text when it has issues so it gives some trouble shooting info
REM #
REM #########################################################################################
   
REM VAR Section
SET HOBBIT_PATH="C:\Program Files\xymon32"
SET HOBBIT_LP_DRIVE_FILE="C:\Program Files\xymon32\ext\netdrive\lp_hobbit_drive_vars.bat"

SET HOBBIT_LOG=logs\netdrive.log
SET HOBBIT_NET_DRIVES_FILE=net_drives
SET HEADER=Drive	Total	Used	Free	Useage	UNC Path
SET STATS_MESSAGE=NA
SET BB_COLOR=GREEN
SET BB_REPORT_FREQ=360
SET RUN_TIME=NA
SET RUN_MOTH=NA
SET RUN_YEAR=NA
SET RUN_DAY=NA
SET RUN_DAY_WEEK=NA
SET LDRIVE=NA
SET PDRIVE=NA
SET LDRIVE_USEAGE=0
SET LDRIVE_USED=0
SET LDRIVE_FREE=0
SET LDRIVE_TOTAL=0
SET PDRIVE_USEAGE=0
SET PDRIVE_USED=0
SET PDRIVE_FREE=0
SET PDRIVE_TOTAL=0
SET DRIVE_ISSUE_FOUND=NO

SET SCRIPT_VERSION=1.4


REM Change the USE_GB_VALUES to YES to use values in GB, other wise these will default to PRECENTAGE values for Example:
REM SET USE_GB_VALUES=YES
SET USE_GB_VALUES=NO
SET /A WARNING_LEVEL=10
SET /A CRITICAL_LEVEL=5

REM SET USE_GB_VALUES=NO
REM SET /A WARNING_LEVEL=90
REM SET /A CRITICAL_LEVEL=95

REM #########################################################################################   
REM Check to see if we can find the UNC path info for the L/P drives for this server/site
if exist %HOBBIT_LP_DRIVE_FILE% call %HOBBIT_LP_DRIVE_FILE%
if NOT EXIST %HOBBIT_LP_DRIVE_FILE% (
   echo %0: Counld not find %HOBBIT_LP_DRIVE_FILE%, exiting >> %HOBBIT_LOG%
   goto :EOF)
   
REM Now lets check to see if dateu and our vbs scripts exists
IF NOT EXIST %HOBBIT_PATH%\ext\netdrive\dateu.exe (
   echo %0: Could not find %HOBBIT_PATH%\ext\netdrive\dateu.exe, exiting >> %HOBBIT_PATH%\%HOBBIT_LOG%
   goto :EOF)   
IF NOT EXIST  %HOBBIT_PATH%\ext\netdrive\hobbbit_lp_free_space_check.vbs (
   echo %0: Could not find %HOBBIT_PATH%\ext\netdrive\hobbbit_lp_free_space_check.vbs, exiting >> %HOBBIT_PATH%\%HOBBIT_LOG%
   goto :EOF)
   

REM Get Date/Time when we ran this script
FOR /f "tokens=*" %%G IN ('"%HOBBIT_PATH%\ext\netdrive\dateu.exe" +%%a') DO SET RUN_DAY_WEEK=%%G
FOR /f "tokens=*" %%G IN ('"%HOBBIT_PATH%\ext\netdrive\dateu.exe" +%%b') DO SET RUN_MONTH=%%G
FOR /f "tokens=*" %%G IN ('"%HOBBIT_PATH%\ext\netdrive\dateu.exe" +%%d') DO SET RUN_DAY=%%G
FOR /f "tokens=*" %%G IN ('"%HOBBIT_PATH%\ext\netdrive\dateu.exe" +%%G') DO SET RUN_YEAR=%%G
FOR /f "tokens=*" %%G IN ('"%HOBBIT_PATH%\ext\netdrive\dateu.exe" +%%H:%%M:%%S') DO SET RUN_TIME=%%G


REM Do a quick test here to ensure L/P drives exist and the UNC paths are not NAs
IF %LDRIVE% == NA (
  SET STATS_MESSAGE=L Drive Usage UNC path is not defined!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF %PDRIVE% == NA (
  SET STATS_MESSAGE=P Drive Usage UNC path is not defined!
  SET BB_COLOR=RED
  GOTO :CONTINUE )      
  

REM Call the VB Script that reports the status, the free spacee, total space and usage
FOR /f "tokens=1,2,3,4,5,6,7,8,9,10 delims=," %%a IN ('cscript %HOBBIT_PATH%\ext\netdrive\hobbbit_lp_free_space_check.vbs %WARNING_LEVEL% %CRITICAL_LEVEL% %PDRIVE% %LDRIVE% %USE_GB_VALUES% //nologo') DO (
  SET PSTATUS==%%a
  SET LSTATUS=%%b
  SET PDRIVE_FREE=%%c
  SET LDRIVE_FREE=%%d
  SET PDRIVE_TOTAL=%%e
  SET LDRIVE_TOTAL=%%f
  SET PDRIVE_USEAGE=%%g
  SET LDRIVE_USEAGE=%%h
  SET PDRIVE_USED=%%i
  SET LDRIVE_USED=%%j)


REM Do a sanity check to ensure we have emtpy, if do mark as bad!
 IF {%PDRIVE_FREE%} == {} (
  SET STATS_MESSAGE=P Drive Free Space NOT Detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_FREE%} == {} (
  SET STATS_MESSAGE=L Drive Free Space NOT Detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )   
 IF {%PDRIVE_TOTAL%} == {} (
  SET STATS_MESSAGE=P Drive Toatal NOT Detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_TOTAL%} == {} (
  SET STATS_MESSAGE=L Drive Drive Toatal NOT Detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )   
IF {%PDRIVE_USEAGE%} == {} (
  SET STATS_MESSAGE=P Drive Usage or Capacity NOT Detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_USEAGE%} == {} (
  SET STATS_MESSAGE=L Drive Usage or Capacity NOT Detected!!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )   
IF {%PDRIVE_USED%} == {} (
  SET STATS_MESSAGE=P Drive Used or Capacity NOT Detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_USED%} == {} (
  SET STATS_MESSAGE=L Drive Used or Capacity NOT Detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE ) 
  
REM Do a sanity check to ensure we have zero values, if do mark as bad!  
IF {%PDRIVE_FREE%} == {0} (
  SET STATS_MESSAGE=P Drive Free Space 0 size detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_FREE%} == {0} (
  SET STATS_MESSAGE=L Drive Free Space 0 size detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )   
 IF {%PDRIVE_TOTAL%} == {0} (
  SET STATS_MESSAGE=P Drive Toatal Space 0 size detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_TOTAL%} == {0} (
  SET STATS_MESSAGE=L Drive Drive Space 0 size detected!!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )   
IF {%PDRIVE_USEAGE%} == {0} (
  SET STATS_MESSAGE=P Drive Usage or Space 0 size detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_USEAGE%} == {0} (
  SET STATS_MESSAGE=L Drive Usage or Space 0 size detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )   
IF {%PDRIVE_USED%} == {0} (
  SET STATS_MESSAGE=P Drive Used or Space 0 size detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF {%LDRIVE_USED%} == {0} (
  SET STATS_MESSAGE=L Drive Used or Space 0 size detected!!
  SET BB_COLOR=RED
  GOTO :CONTINUE ) 
    
  
REM Set the status/color based on the results  if we used GB values, just to section that had better text to cover those cases
IF %USE_GB_VALUES% == YES GOTO :USE_GB_VALUES
 IF %LSTATUS% == RED (
  SET STATS_MESSAGE=L Capacity, %LDRIVE_USEAGE% , is above Critical Threashod of %CRITICAL_LEVEL%!!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF %PSTATUS% == RED (
  SET STATS_MESSAGE=P Capacity, %PDRIVE_USEAGE% , is above Critical Threashod of %CRITICAL_LEVEL%!!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF %LSTATUS% == YELLOW (
  SET STATS_MESSAGE=L Capacity, %LDRIVE_USEAGE% , is above Warning Threashod of %WARNING_LEVEL%!!!
  SET BB_COLOR=YELLOW
  GOTO :CONTINUE )
IF %PSTATUS% == YELLOW (
  SET STATS_MESSAGE=P Capacity, %PDRIVE_USEAGE% , is abovew Warning Threashod of %WARNING_LEVEL%!!!
  SET BB_COLOR=YELLOW
  GOTO :CONTINUE )     
 
   

:CONTINUE
REM Build the test results
echo %BB_COLOR%+%BB_REPORT_FREQ% LP NET Drive Results on %RUN_DAY_WEEK% %RUN_MONTH% %RUN_DAY% %RUN_TIME% %RUN_YEAR%>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%


if NOT %BB_COLOR% == GREEN (
  echo Issue Dectected!!!! >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo.>>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo %STATS_MESSAGE% >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo P drive info:>>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  FOR /f "tokens=*" %%a IN ('dir /-c %PDRIVE% ^| %SYSTEMROOT%\system32\findstr /C:"bytes"') do echo %%a >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  echo L drive info:>>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  FOR /f "tokens=*" %%a IN ('dir /-c %LDRIVE% ^| %SYSTEMROOT%\system32\findstr /C:"bytes"') do echo %%a >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
  ) ELSE (
  echo L and P Drives usage is GOOD! >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE% )
echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo Using GB Values set to: %USE_GB_VALUES% >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo %HEADER%>>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo L:\	%LDRIVE_TOTAL%	%LDRIVE_USED%	%LDRIVE_FREE%	%LDRIVE_USEAGE%	%LDRIVE%>> %HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo P:\	%PDRIVE_TOTAL%	%PDRIVE_USED%	%PDRIVE_FREE%	%PDRIVE_USEAGE%	%PDRIVE%>> %HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo. >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%
echo Script Version used is: %SCRIPT_VERSION%  >>%HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE%

REM Move the net drive file over to the tmp bin of bbwin so it gets uploaded and clean up
move /Y %HOBBIT_PATH%\ext\netdrive\%HOBBIT_NET_DRIVES_FILE% %HOBBIT_PATH%\tmp
REM Thats it!
GOTO :EOF


REM #########################################################################################
REM Use the GB values to do the check here
:USE_GB_VALUES
 IF %LSTATUS% == RED (
  SET STATS_MESSAGE=L FREE Space, %LDRIVE_FREE%, is below Critical Threashod of %CRITICAL_LEVEL%!!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF %PSTATUS% == RED (
  SET STATS_MESSAGE=P FREE Space, %PDRIVE_FREE%, is below Critical Threashod of %CRITICAL_LEVEL%!!!
  SET BB_COLOR=RED
  GOTO :CONTINUE )
IF %LSTATUS% == YELLOW (
  SET STATS_MESSAGE=L FREE Space, %LDRIVE_FREE%, is below Warning Threashod of %WARNING_LEVEL%!!!
  SET BB_COLOR=YELLOW
  GOTO :CONTINUE )
IF %PSTATUS% == YELLOW (
  SET STATS_MESSAGE=P FREE Space, %PDRIVE_FREE%, is below Warning Threashod of %WARNING_LEVEL%!!!
  SET BB_COLOR=YELLOW
  GOTO :CONTINUE )
GOTO :CONTINUE 